<?php

declare(strict_types=1);

namespace App\Controller\playlet;
use App\Controller\BaseController;
use App\Model\AppletPlayletRel;
use App\Model\Playlet;
use App\Model\PlayletEpisodes;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Common\Response\SystemCode;
use App\Service\playlet\PlayletService;

#[AutoController]
class PlayletController  extends BaseController
{

    #[Inject]
    private PlayletService $playletService;
    /**
     * 短剧列表
     */
    public function Index(RequestInterface $request)
    {
        $data = $this->playletService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);


    }
    /**
     * 短剧详情
     */
    public function View(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $playletInfo = Playlet::query()->select(['id','name','cover','introduction','is_finish','amount','last_episode_number','last_episode_name','pay_start_cid','pay_type','status','likes_number','share_number','hot_number','created_at','updated_at'])->where('id', $id)->first();
            if (empty($playletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $playletInfo);
        }
    }
    /**
     * 短剧创建
     */
    public function Create(RequestInterface $request){

        $result = $this->playletService->createOrUpdate($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '创建成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }
    }
    /**
     * 短剧删除
     */
    public function Delete(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $playletInfo = Playlet::query()->where('id', $id)->first();
            if (empty($playletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            $playletInfo->deleted_at = date('Y-m-d H:i:s');
            $playletInfo->save();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '操作成功');
        }
    }
    /**
     * 短剧更新
     */
    public function Update(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{

            $playletInfo = Playlet::query()->select(['id','name','producer','registration_number','thumbnail','registration_number_url','cover','introduction','is_finish','amount','last_episode_number','last_episode_name','pay_start_cid','pay_type','status'])->where('id', $id)->first();


            if(empty($playletInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            if($request->getMethod() == 'GET'){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$playletInfo);

            }else{
                $result = $this->playletService->createOrUpdate($request->post(),true,$playletInfo);

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

                }
            }
        }
    }

    public function Episodes(int $id){

        if(!empty($id)) {
            $playletInfo = Playlet::query()->select('id')->where('id', $id)->first();
            if (empty($playletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            $playletEpisodesInfo = PlayletEpisodes::query()->select(['playlet_number as value','playlet_number_name as label'])->where(['playlet_id' => $id])->get();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $playletEpisodesInfo);
        }

    }

    public function EpisodesList(int $id){

        if(!empty($id)) {
            $playletInfo = Playlet::query()->select('id')->where('id', $id)->first();
            if (empty($playletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            $playletEpisodesInfo = PlayletEpisodes::query()->select(['playlet_number','playlet_number_name','original_play_url','play_url','wx_original_url','cover'])->where(['playlet_id' => $id])->orderBy('playlet_number','asc')->get();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $playletEpisodesInfo);
        }

    }

    public function batchUpload(int $id,RequestInterface $request){

        $playletInfo = Playlet::query()->select(['id','cover'])->where('id', $id)->first();
        if (empty($playletInfo)) {
            return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
        }
        $result = $this->playletService->batchUpload($request->post(),$id,$playletInfo->cover);
        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '上传成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);
        }
    }
    /**
     * 短剧微信,微信审核接口
     */
    public function AuditDrama(int $id,int $type = 1){

        if(!empty($id)) {
            $playletInfo = Playlet::query()->select(['id','is_finish'])->where('id', $id)->first();
            if (empty($playletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }

            if($playletInfo->is_finish == 0){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧未完结不可以上传微信审核');
            }

            $result =   $this->playletService->auditDrama($id,$type);

            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '短剧剧目审核提交成功');
            }else{
                return $this->response($result['code'],$result['msg'],$result['data']);

            }
        }

    }
}